package com.ebt.m.wiki;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;

import com.ebt.m.R;
import com.ebt.m.widget.HackyViewPager;
import com.ebt.m.widget.TitleHeaderBar;
import com.ebt.m.wiki.adapter.ImagePagerAdapter;

import java.util.ArrayList;


/**
 * @author Damon.Han
 * @Description: 图片预览activity
 */
public class ActImgViewer extends FragmentActivity {
    public static final int requestCode = 9999;
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    private HackyViewPager mPager;
    private TitleHeaderBar headerBar;
    private Context context;
    private String dateStr = "";
    private int pagerPosition;
    private ArrayList<String> urls;
    private ImagePagerAdapter mImagePagerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        context = this;
        setContentView(R.layout.act_img_viewer);
        headerBar = (TitleHeaderBar) findViewById(R.id.lib_common_bar);
        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        urls = (ArrayList<String>) getIntent().getSerializableExtra("data");
        if (urls == null || urls.size() == 0) {
            return;
        }
        mPager = (HackyViewPager) findViewById(R.id.pager);
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int state) {
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                headerBar.setTitle((position + 1) + "/" + urls.size());
            }

        });
        if (urls == null) {
            urls = new ArrayList<String>();
        }
        initData();
        headerBar.getLeftViewContainer().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initData() {
        mImagePagerAdapter = new ImagePagerAdapter(context, urls);
        mPager.setAdapter(mImagePagerAdapter);

        headerBar.setTitle("1/" + urls.size());
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        setResult(FrgProductDesc.IMG_RESULT_CODE);
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }
}
